/**
 * 项目配置管理--项目配置列表的单例对象
 */
var ProjectConfig = {
    id: "ProjectConfigTable",//表格id
    seItem: null,		//选中的第一条记录
    ids: null,//选中的所有记录ids
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ProjectConfig.initColumn = function () {
    var columns = [
        {field: 'selectItem'},
        {title: '编号', field: 'id', align: 'center', valign: 'middle', sortable: true},
        {title: '项目名称', field: 'project_name', align: 'center', valign: 'middle', sortable: true},
        {title: '数据库地址', field: 'db_url', align: 'center', valign: 'middle', sortable: true},
        {title: '数据库登录名', field: 'db_username', align: 'center', valign: 'middle', sortable: true},
        {title: '数据库密码', field: 'db_password', align: 'center', valign: 'middle', sortable: true},
        {title: '数据库类型', field: 'db_type', align: 'center', valign: 'middle', sortable: true}]
    return columns;
};

/**
 * 检查是否选中
 */
ProjectConfig.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ProjectConfig.seItem = selected[0];
        var ids = "";
        for (var i = 0; i < selected.length; i++) {
            var item = selected[i];
            ids = ids + item.id + ",";
        }
        ids = ids.substring(0, ids.length - 1);
        ProjectConfig.ids = ids;
        return true;
    }
};

/**
 * 点击添加项目配置
 */
ProjectConfig.openProjectConfig = function () {
    var index = layer.open({
        type: 2,
        title: '新建项目配置',
        area: ['1000px', '800px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/projectConfig/project_config_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看项目配置详情
 */
ProjectConfig.openProjectConfigDetail = function () {
    if (this.check()) {
        if (isNaN(ProjectConfig.ids)) {
            Feng.info("请不要选择多条记录！");
            return false;
        }
        var index = layer.open({
            type: 2,
            title: '项目配置详情',
            area: ['1000px', '800px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/projectConfig/project_config_update/' + ProjectConfig.seItem.id
        });
        this.layerIndex = index;
    }
};

//测试数据库连接
ProjectConfig.testConnection = function () {
    if (this.check()) {
        if (isNaN(ProjectConfig.ids)) {
            Feng.info("请不要选择多条记录！");
            return false;
        }
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/projectConfig/test_connection", function (data) {
                Feng.success("连接成功!");
            }, function (data) {
                Feng.error(data.responseJSON.message + "!");
            });
            ajax.set("projectConfigId", ProjectConfig.seItem.id);
            ajax.start();
        };
        Feng.confirm("是否确认测试?", operation);
    }
};
$(function () {
    var defaultColunms = ProjectConfig.initColumn();
    var table = new BSTable(ProjectConfig.id, "/projectConfig/list", defaultColunms);
    table.setPaginationType("server");
    ProjectConfig.table = table.init();
});